<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>basic_yield_context::operator[]</title>
    <link rel="stylesheet" href="../../../boostbook.css" type="text/css">
    <meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
    <link rel="home" href="../../../index.html" title="Asio">
    <link rel="up" href="../basic_yield_context.html" title="basic_yield_context">
    <link rel="prev" href="caller_type.html" title="basic_yield_context::caller_type">
    <link rel="next" href="../bind_allocator.html" title="bind_allocator">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%">
    <tr>
        <td valign="top"><img alt="asio C++ library" width="250" height="60" src="../../../asio.png"></td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="caller_type.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                            href="../basic_yield_context.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="../bind_allocator.html"><img src="../../../next.png" alt="Next"></a>
</div>
<div class="section">
    <div class="titlepage">
        <div>
            <div><h4 class="title">
                <a name="asio.reference.basic_yield_context.operator_lb__rb_"></a><a class="link"
                                                                                     href="operator_lb__rb_.html"
                                                                                     title="basic_yield_context::operator[]">basic_yield_context::operator[]</a>
            </h4></div>
        </div>
    </div>
    <p>
        <a class="indexterm" name="asio.indexterm.basic_yield_context.operator_lb__rb_"></a>
        Return
        a yield context that sets the specified <a class="link" href="../error_code.html" title="error_code"><code
            class="computeroutput"><span class="identifier">error_code</span></code></a>.
    </p>
    <pre class="programlisting"><span class="identifier">basic_yield_context</span> <span
            class="keyword">operator</span><span class="special">[](</span>
    <span class="identifier">asio</span><span class="special">::</span><span class="identifier">error_code</span> <span
                class="special">&amp;</span> <span class="identifier">ec</span><span class="special">)</span> <span
                class="keyword">const</span><span class="special">;</span>
</pre>
    <p>
        By default, when a yield context is used with an asynchronous operation,
        a non-success <a class="link" href="../error_code.html" title="error_code"><code class="computeroutput"><span
            class="identifier">error_code</span></code></a>
        is converted to <a class="link" href="../system_error.html" title="system_error"><code
            class="computeroutput"><span class="identifier">system_error</span></code></a> and thrown. This
        operator may be used to specify an <a class="link" href="../error_code.html" title="error_code"><code
            class="computeroutput"><span class="identifier">error_code</span></code></a> object that should
        instead be set with the asynchronous operation's result. For example:
    </p>
    <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span
            class="keyword">typename</span> <span class="identifier">Handler</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">my_coroutine</span><span class="special">(</span><span
                class="identifier">basic_yield_context</span><span class="special">&lt;</span><span class="identifier">Handler</span><span
                class="special">&gt;</span> <span class="identifier">yield</span><span class="special">)</span>
<span class="special">{</span>
  <span class="special">...</span>
  <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span
                class="identifier">n</span> <span class="special">=</span> <span
                class="identifier">my_socket</span><span class="special">.</span><span class="identifier">async_read_some</span><span
                class="special">(</span><span class="identifier">buffer</span><span class="special">,</span> <span
                class="identifier">yield</span><span class="special">[</span><span class="identifier">ec</span><span
                class="special">]);</span>
  <span class="keyword">if</span> <span class="special">(</span><span class="identifier">ec</span><span class="special">)</span>
  <span class="special">{</span>
    <span class="comment">// An error occurred.</span>
  <span class="special">}</span>
  <span class="special">...</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%">
    <tr>
        <td align="left"></td>
        <td align="right">
            <div class="copyright-footer">Copyright © 2003-2022 Christopher M. Kohlhoff<p>
                Distributed under the Boost Software License, Version 1.0. (See accompanying
                file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
            </p>
            </div>
        </td>
    </tr>
</table>
<hr>
<div class="spirit-nav">
    <a accesskey="p" href="caller_type.html"><img src="../../../prev.png" alt="Prev"></a><a accesskey="u"
                                                                                            href="../basic_yield_context.html"><img
        src="../../../up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../home.png"
                                                                                            alt="Home"></a><a
        accesskey="n" href="../bind_allocator.html"><img src="../../../next.png" alt="Next"></a>
</div>
</body>
</html>
